# 文件名：main.py
# 用途：加解密
# Gitee：https://gitee.com/binzhijie/Character-encryption-and-decryption
#
#           _____                    _____                    _____                    _____
#          /\    \                  /\    \                  /\    \                  /\    \
#         /::\    \                /::\    \                /::\    \                /::\    \
#        /::::\    \              /::::\    \              /::::\    \              /::::\    \
#       /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \
#      /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \
#     /:::/  \:::\    \        /:::/__\:::\    \        /:::/__\:::\    \        /:::/  \:::\    \
#    /:::/    \:::\    \      /::::\   \:::\    \      /::::\   \:::\    \      /:::/    \:::\    \
#   /:::/    / \:::\    \    /::::::\   \:::\    \    /::::::\   \:::\    \    /:::/    / \:::\    \
#  /:::/    /   \:::\    \  /:::/\:::\   \:::\    \  /:::/\:::\   \:::\    \  /:::/    /   \:::\ ___\
# /:::/____/     \:::\____\/:::/__\:::\   \:::\____\/:::/  \:::\   \:::\____\/:::/____/     \:::|    |
# \:::\    \      \::/    /\:::\   \:::\   \::/    /\::/    \:::\  /:::/    /\:::\    \     /:::|____|
#  \:::\    \      \/____/  \:::\   \:::\   \/____/  \/____/ \:::\/:::/    /  \:::\    \   /:::/    /
#   \:::\    \               \:::\   \:::\    \               \::::::/    /    \:::\    \ /:::/    /
#    \:::\    \               \:::\   \:::\____\               \::::/    /      \:::\    /:::/    /
#     \:::\    \               \:::\   \::/    /               /:::/    /        \:::\  /:::/    /
#      \:::\    \               \:::\   \/____/               /:::/    /          \:::\/:::/    /
#       \:::\    \               \:::\    \                  /:::/    /            \::::::/    /
#        \:::\____\               \:::\____\                /:::/    /              \::::/    /
#         \::/    /                \::/    /                \::/    /                \::/____/
#          \/____/                  \/____/                  \/____/                  ~~
#


import base64
from tkinter import *
# from tkinter import messagebox

# try:
#     from pyperclip import copy
# except:
#     import os
#     print('正在安装依赖...')
#     os.system('python -m pip install pyperclip')
#     from pyperclip import copy



class Endecryption:
    def __init__(self):
        self.__aaa = {' ': 'gUo', '1': 'noK', '2': 'FNA', '3': 'Rfc', '4': 'EwU', '5': 'tar', '6': 'DDY', '7': 'zoM',
                      '8': 'VeB', '9': 'bVK', '0': 'Ymb', 'q': 'IXf', 'w': 'PnV', 'e': 'GHp', 'r': 'Lmv', 't': 'dyX',
                      'y': 'Sgt', 'u': 'mEk', 'i': 'KIX', 'o': 'lHw', 'p': 'eLT', 'a': 'hmL', 's': 'EAV', 'd': 'jur',
                      'f': 'RJP', 'g': 'DZm', 'h': 'npK', 'j': 'flK', 'k': 'ttH', 'l': 'QtG', 'z': 'icL', 'x': 'plM',
                      'c': 'CPo', 'v': 'DGt', 'b': 'hnj', 'n': 'Hjl', 'm': 'UMc', 'Q': 'qBw', 'W': 'izi', 'E': 'lQl',
                      'R': 'UMZ', 'T': 'ukq', 'Y': 'TDq', 'U': 'SaW', 'I': 'aFC', 'O': 'KLW', 'P': 'idq', 'A': 'fXz',
                      'S': 'nFs', 'D': 'WOD', 'F': 'tVn', 'G': 'uWC', 'H': 'Mnd', 'J': 'jqs', 'K': 'cdX', 'L': 'UFN',
                      'Z': 'SuD', 'X': 'xRS', 'C': 'XiH', 'V': 'lqd', 'B': 'oqq', 'N': 'qqh', 'M': 'eRo', '~': 'blr',
                      '!': 'sJK', '@': 'uUj', '#': 'nvb', '$': 'QjO', '%': 'snk', '^': 'IZP', '&': 'LGp', '*': 'HuD',
                      '(': 'MzO', ')': 'wlV', '`': 'DgW', '-': 'Lea', '_': 'Mqh', '=': 'EaG', '+': 'gEy', '[': 'LPQ',
                      '{': 'EyK', ']': 'xsO', '}': 'QOa', r'\\': 'ECJ', '|': 'Czy', ';': 'OZz', ':': 'sjW', "'": 'CGw',
                      '"': 'kLz', ',': 'Iuu', '<': 'rnT', '.': 'vRc', '>': 'LlR', '/': 'aKP', '?': 'czE',}

        self.__bbb = {'gUo': ' ', 'noK': '1', 'FNA': '2', 'Rfc': '3', 'EwU': '4', 'tar': '5', 'DDY': '6', 'zoM': '7',
                      'VeB': '8', 'bVK': '9', 'Ymb': '0', 'IXf': 'q', 'PnV': 'w', 'GHp': 'e', 'Lmv': 'r', 'dyX': 't',
                      'Sgt': 'y', 'mEk': 'u', 'KIX': 'i', 'lHw': 'o', 'eLT': 'p', 'hmL': 'a', 'EAV': 's', 'jur': 'd',
                      'RJP': 'f', 'DZm': 'g', 'npK': 'h', 'flK': 'j', 'ttH': 'k', 'QtG': 'l', 'icL': 'z', 'plM': 'x',
                      'CPo': 'c', 'DGt': 'v', 'hnj': 'b', 'Hjl': 'n', 'UMc': 'm', 'qBw': 'Q', 'izi': 'W', 'lQl': 'E',
                      'UMZ': 'R', 'ukq': 'T', 'TDq': 'Y', 'SaW': 'U', 'aFC': 'I', 'KLW': 'O', 'idq': 'P', 'fXz': 'A',
                      'nFs': 'S', 'WOD': 'D', 'tVn': 'F', 'uWC': 'G', 'Mnd': 'H', 'jqs': 'J', 'cdX': 'K', 'UFN': 'L',
                      'SuD': 'Z', 'xRS': 'X', 'XiH': 'C', 'lqd': 'V', 'oqq': 'B', 'qqh': 'N', 'eRo': 'M', 'blr': '~',
                      'sJK': '!', 'uUj': '@', 'nvb': '#', 'QjO': '$', 'snk': '%', 'IZP': '^', 'LGp': '&', 'HuD': '*',
                      'MzO': '(', 'wlV': ')', 'DgW': '`', 'Lea': '-', 'Mqh': '_', 'EaG': '=', 'gEy': '+', 'LPQ': '[',
                      'EyK': '{', 'xsO': ']', 'QOa': '}', 'ECJ': r'\\', 'Czy': '|', 'OZz': ';', 'sjW': ':', 'CGw': "'",
                      'kLz': '"', 'Iuu': ',', 'rnT': '<', 'vRc': '.', 'LlR': '>', 'aKP': '/', 'czE': '?'}

    def __split_string(self, s, interval=1):
        result = []
        for i in range(0, len(s), interval):
            result.append(s[i:i + interval])
        while '\n' in result:
            result.remove('\n')
        while '\n\n' in result:
            result.remove('\n\n')
        while '\n\n\n' in result:
            result.remove('\n\n\n')

        return result

    def encryption(self,content):
        old = content
        old = base64.b64encode(old.encode('utf-8')).decode('utf-8')
        new = ''
        for i in old:
            new += self.__aaa[i]
        return new

    def decryption(self,content):
        old = content
        new = ''
        old = self.__split_string(old,3)
        for i in old:
            new += self.__bbb[i]
        new = base64.b64decode(new.encode('utf-8')).decode('utf-8')
        return new

if __name__ == '__main__':
    def set_windows_x_y(window, window_width, window_height):
        # 设置窗口的宽度和高度
        window_width = window_width
        window_height = window_height

        screen_width = window.winfo_screenwidth()
        screen_height = window.winfo_screenheight()

        # 计算窗口的左上角位置，使其居中显示
        x_position = (screen_width - window_width) / 2
        y_position = (screen_height - window_height) / 2
        # 设置窗口的位置和大小
        window.wm_geometry(f"{str(window_width)}x{str(window_height)}+{str(int(x_position))}+{str(int(y_position))}")

    def ent():
        # try:
        ed = Endecryption()
        old = first_input.get('0.0','end')
        new = ed.encryption(old)
        last_input.delete('0.0','end')
        last_input.insert('insert', new)
        # if messagebox.askyesno('Tips:','加密成功，是否复制？'):
        #     copy(new)
        # except:
        #     messagebox.showerror('Error','加密失败')


    def det():
        # try:
        ed = Endecryption()
        old = first_input.get('0.0','end')
        new = ed.decryption(old)
        last_input.delete('0.0','end')
        last_input.insert('insert', new)
        # if messagebox.askyesno('Tips:','解密成功，是否复制？'):
        #     copy(new)
        # except:
        #     messagebox.showerror('Error','解密失败')

    window = Tk()
    window.title('CEAD')
    set_windows_x_y(window,500,400)
    window.resizable(0,0)

    home_title =Label(window,text='CEAD',font=('黑体',25,'bold'))
    first_input = Text(window,width=65,height=10,wrap=WORD)
    enbutton = Button(window,text='加密',font=('黑体',15,'bold'),width=10,command=ent)
    debutton = Button(window, text='解密', font=('黑体', 15, 'bold'), width=10,command=det)
    last_input = Text(window,width=65,height=10,wrap=WORD)

    home_title.pack()
    first_input.pack()
    enbutton.pack()
    debutton.pack()
    last_input.pack()

    window.mainloop()